package com.hsy.baoinbao.modules;

import android.content.Intent;
import android.os.Bundle;
import android.os.Message;
import android.widget.TextView;

import com.deparse.dpframe.utils.DPLogUtil;
import com.hsy.baoinbao.R;
import com.hsy.baoinbao.modules.common.CommonBaseActivity;
import com.hsy.baoinbao.modules.login.LoginActivity;
import com.hsy.baoinbao.modules.main.ui.activity.MainActivity;
import com.hsy.baoinbao.modules.order.activity.DetailReceivingActivity;
import com.hsy.baoinbao.modules.order.adapter.ReceivingAdapter;
import com.hsy.baoinbao.utils.CommonUtils;
import com.tencent.android.tpush.XGPushClickedResult;
import com.tencent.android.tpush.XGPushManager;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by hsy on 2016/3/24.
 */
public class SplashActivity extends CommonBaseActivity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_splash);
        List<TextView> data = new ArrayList<>();
//        ReceivingAdapter<TextView> adapter = new ReceivingAdapter<>(data);
//        adapter.update(data);

    }

    @Override
    protected void onStart() {
        super.onStart();

        XGPushClickedResult click = XGPushManager.onActivityStarted(this);
        if (click != null) {
            String customContent = click.getCustomContent();
            DPLogUtil.d("customContent=" + customContent);
            if (customContent != null && customContent.length() != 0) {
                finish();
                try {
                    JSONObject result = new JSONObject(customContent);
                    String id = result.optString("id");
                    Intent intent = new Intent(this, DetailReceivingActivity.class);
                    intent.putExtra("fromNotification", true);
                    intent.putExtra("id", id);
                    startActivity(intent);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        }

    }

    @Override
    protected void onResume() {
        super.onResume();
//        postDelayed(new Runnable() {
//            @Override
//            public void run() {
//                DPLogUtil.d(CommonUtils.getToken(getApplicationContext()));
//                if (CommonUtils.checkLogin(getApplicationContext())) {
//                    startActivityWithTaskCleared(MainActivity.class);
//                    finish();
//                } else {
//                    startActivityWithTaskCleared(LoginActivity.class);
//                    finish();
//                }
//            }
//        }, 3000);
        sendEmptyMessageDelayed(0, 3000);
    }

    @Override
    protected void handleMessage(Message msg) {
        super.handleMessage(msg);
        if (0 == msg.what) {
            if (CommonUtils.checkLogin(getApplicationContext())) {
                startActivityWithTaskCleared(MainActivity.class);
            } else {
                startActivityWithTaskCleared(LoginActivity.class);
            }
            finish();
        }
    }

    @Override
    protected void onStop() {
        removeMessage(0);
        XGPushManager.onActivityStoped(this);
        super.onStop();
    }
}
